package com.clwl.supervise.superviseapp.controller.base;

import com.clwl.supervise.superviseapp.entity.base.SysDictData;
import com.clwl.supervise.superviseapp.entity.result.ResultEntity;
import com.clwl.supervise.superviseapp.entity.scan.TjFoodTypeTree;
import com.clwl.supervise.superviseapp.entity.scan.TjFoodtype;
import com.clwl.supervise.superviseapp.service.base.ScanInfoService;
import com.clwl.supervise.superviseapp.util.baiduAPI.BaiduAipUtil;
import com.clwl.supervise.superviseapp.util.baiduAPI.ScanUtil;
import org.apache.commons.lang3.StringUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 扫一扫
 */
@RestController
@RequestMapping("scan")
public class ScanInfoController {

    @Resource
    private ScanInfoService scanInfoService;

    /**
     * 获取扫描原料使用的原料列表
     * @param type 对应2760类别(对应的字典中的类别)
     * @return
     */
    @GetMapping("getMaterialsTypeList")
    public ResultEntity getMaterialsTypeList(String type){
        List<TjFoodTypeTree> foodTypeTreeList = scanInfoService.getMaterialsTypeList(type);
        return ResultEntity.success(foodTypeTreeList);
    }

    /**
     * 获取2760判定标准
     * @return
     */
    @GetMapping("get2760TypeList")
    public ResultEntity get2760TypeList(){
        List<SysDictData> dataList = scanInfoService.get2760TypeList();
        dataList.iterator().forEachRemaining(sysDictData -> {
            if(StringUtils.equals("3",sysDictData.getDictValue())){
                sysDictData.setCheckboxType("1");
            } else {
                sysDictData.setCheckboxType("0");
            }
        });
        return ResultEntity.success(dataList);
    }


    /**
     * 获取扫描原料使用的原料一级列表
     * @return
     */
    @GetMapping("getMaterialsTypeListByParent")
    public ResultEntity getMaterialsTypeListByParent(String parentId){
        List<TjFoodtype> foodTypeTreeList = scanInfoService.getMaterialsTypeListByParent(parentId);
        return ResultEntity.success(foodTypeTreeList);
    }


    /**
     * 扫描识别
     * @param file 文件流
     * @param type 1-证照;2-OCR识别;3-物品识别;4-条码/二维码 ;5-原料识别
     * @param foodType  原料识别的时候要传递此参数
     * @return
     */
    @RequestMapping("getResult")
    private ResultEntity scanDocumentFoodType(MultipartFile file, String type, String foodType) {
        try {
            Map<String, Object> re = new HashMap<>();
            String audioUrl = BaiduAipUtil.updateSpotImage(file);
            switch (type){
                case "1":
                    //识别证照
                    org.json.JSONObject resultJSONObject = BaiduAipUtil.businessLicense(audioUrl);
                    if (resultJSONObject != null) {
                        String companyName = resultJSONObject.getJSONObject("单位名称") == null ? "" : resultJSONObject.getJSONObject("单位名称").getString("words");
                        String listensNo = resultJSONObject.getJSONObject("社会信用代码") == null ? "" : resultJSONObject.getJSONObject("社会信用代码").getString("words");
                        re.put("companyName", companyName);
                        re.put("listensNo", listensNo);
                        re.put("text", resultJSONObject);

                    }
                    break;
                default:
                    re = ScanUtil.scanPhotoTianjin(audioUrl,type,foodType);
                    break;
            }
            return ResultEntity.success(re);
        } catch (Exception e) {
            e.printStackTrace();
            return ResultEntity.error("识别内容失败");
        }
    }

    /**
     * 通过选择的原料类型和识别完成的原料信息进行识别
     * @param classFoodType  分类code
     * @param rawMaterialFoodList 原料识别信息
     * @return
     */
    @PostMapping("getMaterialsScanResult")
    public ResultEntity getMaterialsScanResult(String classFoodType,String rawMaterialFoodList){
        Map<String,Object> additivesList = scanInfoService.getMaterialsScanResult(classFoodType,rawMaterialFoodList);
        return ResultEntity.success(additivesList);
    }


}
